/* 
    首页子应用
*/
const express = require('express');

// 导航类目中间件
const category = require('../middleware/category');
// 文章中间件
const article = require('../middleware/article');
// 权限中间件，获取用户信息
const auth = require('../middleware/auth');

// 首页子应用
const indexApp = express();
// 注册权限模块在indexApp
indexApp.use(auth.getUser);

// 加载首页页面
indexApp.get('/',[article.getCount],(req,res,next)=>{
    // 用户信息，文章信息
    let {articleCount} = req;
    let size = 6;
    req.page = {};
    // 文章总数
    req.page.count = articleCount;
    // 总页数
    req.page.total = Math.ceil(req.page.count/size);  
    // 当前页数
    req.page.p = req.query.p ? req.query.p : 1;
    req.page.p = req.page.p > req.page.total ? req.page.total : req.page.p;
    req.page.p = req.page.p < 1 ? 1:req.page.p;

    res.start = (req.page.p - 1)*size;
    res.size = size;
    next();
},[article.getHot,article.getList,category.getList,article.getPage],(req,res) =>{
    let {hots,category,user,pageList} = req;
    let article = pageList;
    res.render('index',{hots,article,category,user,page:req.page,
        title:'首页'
    });
});

// 导出
module.exports = indexApp;